package icu.yougan.trade.controller;

import icu.yougan.trade.entity.IdleItem;
import icu.yougan.trade.entity.User;
import icu.yougan.trade.enums.ErrorMsg;
import icu.yougan.trade.service.AuthenticationService;
import icu.yougan.trade.service.IdleItemService;
import icu.yougan.trade.service.UserService;
import icu.yougan.trade.vo.R;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * @Author:油柑
 * @changeByLyh
 * @Date:2024/5/18 22:29
 * @Version:v1.0.0
 * @Description:接口访问控制
 **/

@CrossOrigin
@RestController
@RequestMapping("idle")
public class IdleItemController {

    @Resource
    private IdleItemService idleItemService;
    @Resource
    private UserService userService;
    @Resource
    private AuthenticationService authenticationService;
    /**
     * 添加闲置商品
     * @param authentication 认证对象容器
     * @param idleItem 闲置id
     * @return 结果
     */
    @PostMapping("add")
    public R addIdleItem(
            Authentication authentication,
            @RequestBody IdleItem idleItem
    ) throws ParseException {
        String accountNumber = authentication.getName();
        User user = userService.getUserByAccountNum(accountNumber);
        idleItem.setUserId(Long.valueOf(user.getId()));
        idleItem.setIdleStatus((byte) 4);

        // 使用 Asia/Shanghai 时区的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String cstTime = sdf.format(new Date());

        // 解析为 Date 对象（确保存入数据库的时间是 CST）
        idleItem.setReleaseTime(sdf.parse(cstTime));

        if (idleItemService.addIdleItem(idleItem)) {
            return R.success(idleItem);
        }
        return R.fail(ErrorMsg.SYSTEM_ERROR);
    }

    /**
     * 获取单个闲置信息
     *
     * @param id 闲置id
     * @return 闲置信息
     */
    @GetMapping("info")
    public R getIdleItem(@RequestParam Long id,Authentication authentication) {
        User userByAccountNum = userService.getUserByAccountNum(authentication.getName());
        return R.success(idleItemService.getIdleItem(id,userByAccountNum.getId()));
    }

    /**
     * 查询所有闲置信息
     * @param authentication 认证对象容器
     * @return 闲置信息
     */
    @GetMapping("all")
    public R getAllIdleItem(
            Authentication authentication
    ) {
        User user = authenticationService.getUserIdByAuthentication(authentication);
        return R.success(idleItemService.getAllIdelItem(Long.valueOf(user.getId())));
    }

    /**
     * 获取热门商品
     * @param p
     * @param n
     * @return
     */
    @GetMapping("findByHot")
    public R findIdleItem( @RequestParam(value = "page", required = false) Integer p,
                           @RequestParam(value = "nums", required = false) Integer n)
    {
        return R.success(idleItemService.getIdleItemByHot(p,n));
    }
    /**
     * 查询限制
     *
     * @param findValue 值
     * @param page      分页大小
     * @param nums      页数
     * @return 闲置商品信息
     */
    @GetMapping("find")
    public R findIdleItem(
            @RequestParam(value = "findValue", required = false) String findValue,
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "nums", required = false) Integer nums
    ) {
        if (null == findValue) {
            findValue = "";
        }
        int p = 1;
        int n = 8;
        if (null != page) {
            p = page > 0 ? page : 1;
        }
        if (null != nums) {
            n = nums > 0 ? nums : 8;
        }
        return R.success(idleItemService.findIdleItem(findValue, p, n));
    }
    @GetMapping("sortByPrice")
    public R IdleItemSortByPrice( @RequestParam(value = "sort", required = false) String sort,
                         @RequestParam(value = "page", required = false) Integer page,
                         @RequestParam(value = "nums", required = false) Integer nums)
    {
        if (null == sort) {
            sort = "";
        }
        int p = 1;
        int n = 8;
        if (null != page) {
            p = page > 0 ? page : 1;
        }
        if (null != nums) {
            n = nums > 0 ? nums : 8;
        }
        return R.success(idleItemService.sortIdleItemByPrice(sort,p,n));
    }
    /**
     * @param findValue 值
     * @param page      分页大小
     * @param nums      页数
     * @return 闲置商品信息
     */
    @GetMapping("hot")
    public R getHotItem( @RequestParam(value = "findValue", required = false) String findValue,
                         @RequestParam(value = "page", required = false) Integer page,
                         @RequestParam(value = "nums", required = false) Integer nums)
    {
        if (null == findValue) {
            findValue = "";
        }
        int p = 1;
        int n = 8;
        if (null != page) {
            p = page > 0 ? page : 1;
        }
        if (null != nums) {
            n = nums > 0 ? nums : 8;
        }
        return R.success(idleItemService.findIdleItem(findValue, p, n));
    }
    /**
     * 根据价格区间寻找物品
     * @param minPrice
     * @param maxPrice
     * @param page
     * @param nums
     * @return
     */
    @GetMapping("findByPrice")
    public R findIdleItemByPrice(@RequestParam(value = "minPrice", required = false) Double minPrice,
                                 @RequestParam(value = "maxPrice", required = false) Double maxPrice,
                                 @RequestParam(value = "page", required = false) Integer page,
                                 @RequestParam(value = "nums", required = false) Integer nums)
    {
        int p = 1;
        int n = 8;
        if (null != page) {
            p = page > 0 ? page : 1;
        }
        if (null != nums) {
            n = nums > 0 ? nums : 8;
        }
        return R.success(idleItemService.findIdleItemByPrice(minPrice,maxPrice, p, n));
    }

    /**
     * 混合方式查询
     * @param minPrice
     * @param maxPrice
     * @param page
     * @param nums
     * @return
     */
    @GetMapping("findByMix")
    public R findIdleItemByPrice(@RequestParam(value = "minPrice", required = false) Double minPrice,
                                 @RequestParam(value = "maxPrice", required = false) Double maxPrice,
                                 @RequestParam(value = "idleLabel", required = true) Integer idleLabel,
                                 @RequestParam(value = "page", required = false) Integer page,
                                 @RequestParam(value = "nums", required = false) Integer nums)
    {
        int p = 1;
        int n = 8;
        if (null != page) {
            p = page > 0 ? page : 1;
        }
        if (null != nums) {
            n = nums > 0 ? nums : 8;
        }
        return R.success(idleItemService.findIdleItemByMix(minPrice,maxPrice,idleLabel,p, n));
    }
    /**
     * 查询标签
     *
     * @return 结果
     */
    @GetMapping("lable")
    public R findIdleItemByLabel(
            @RequestParam(value = "idleLabel", required = true) Integer idleLabel,
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "nums", required = false) Integer nums
    ) {
        int p = 1;
        int n = 8;
        if (null != page) {
            p = page > 0 ? page : 1;
        }
        if (null != nums) {
            n = nums > 0 ? nums : 8;
        }
        return R.success(idleItemService.findIdleItemByLabel(idleLabel, p, n));
    }

    /**
     * 更新信息
     *
     * @param idleItem 限制id
     * @return 更新结果
     */
    @PostMapping("update")
    public R updateIdleItem(
            Authentication authentication,
            @RequestBody IdleItem idleItem
    ) {
        User user = authenticationService.getUserIdByAuthentication(authentication);
        idleItem.setUserId(Long.valueOf(user.getId() ));
        if (idleItemService.updateIdleItem(idleItem)) {
            return R.success();
        }
        return R.fail(ErrorMsg.SYSTEM_ERROR);
    }
}
